public class StampAFRCtrlEx {

	private Case c;
    private String debugStr = '';
    public Boolean getInDebugMode() { return false; }
    public String getDebugStr() { return debugStr; }
    public StampAFRCtrlEx() {}
    public PageReference GotoRetUrl() {
    	String retURL = ApexPages.CurrentPage().getParameters().get('retURL');
    	if ((retURL == null) || (retURL == '')) retURL = '/500';
        
        if (getInDebugMode()) {
        	return null;
    	} else {
	        PageReference pr = new PageReference(retURL);
	        pr.setRedirect(true);
	        return pr;
        }
    }
    public PageReference init() {
        try {  
        	this.c = [select Id,ENT_2__Actual_First_Response__c from case where Id=:ApexPages.CurrentPage().getParameters().get('id')];
        	if (this.c.Actual_First_Response__c==null) {
        		this.c.Actual_First_Response__c = system.now();
        		update this.c;
        		this.debugStr += 'Case is now stamped.\n';
        	} else {
            	this.debugStr += 'Case was already stamped.\n';
        	}
        } catch(Exception e) { 
            this.debugStr += 'Case was null.\n';
        }
		
        return (this.GotoRetUrl());
    }

    static void assDatetimesEqual(Datetime dt1, Datetime dt2) {
        string msg = 'dt1='+dt1.format()+' <> dt2='+dt2.format();
        system.assert( (dt1==dt2) || (dt1>dt2 && dt1.addMinutes(-1)<dt2) || (dt1<dt2 && dt1.addMinutes(1)>dt2) , msg );
    }

    static Case mkCase(Datetime AFR) {
        Case c = new Case(ENT_2__Actual_First_Response__c = AFR);
        insert c;
        
        // Unnrelated triggers or workflow may have set the AFR while we weren't looking, so our last act is to reset it.
//        c.ENT_2__Actual_First_Response__c = AFR; 
//        update c;
        system.assertEquals([select ENT_2__Actual_First_Response__c from Case where Id=:c.Id].ENT_2__Actual_First_Response__c , AFR);
        
        return c;
    }
    static testMethod void testNormalCase_ExpectingUpdateBecauseUnstamped() {
        Case c = mkCase(null);
        Test.setCurrentPage(Page.StampAFR);
        ApexPages.CurrentPage().getParameters().putAll( new Map<String,String>{'id'=>c.Id,'retURL'=>''} );
        PageReference pr = (new StampAFRCtrlEx()).init();
        assDatetimesEqual( system.now() , [select ENT_2__Actual_First_Response__c from Case where Id=:c.Id].ENT_2__Actual_First_Response__c );
    }
    static testMethod void testNormalCase_NotExpectingUpdateBecauseStampedAlready() {
    	Datetime dtFut = System.now().addSeconds(1);
        Case c = mkCase(dtFut);
        Test.setCurrentPage(Page.StampAFR);
        ApexPages.CurrentPage().getParameters().putAll( new Map<String,String>{'id'=>c.Id,'retURL'=>''} );
        PageReference pr = (new StampAFRCtrlEx()).init();
        system.assertEquals( dtFut , [select ENT_2__Actual_First_Response__c from Case where Id=:c.Id].ENT_2__Actual_First_Response__c );
    }
    static testMethod void testTryStampAction_NoParams() {
        Test.setCurrentPage(Page.StampAFR);
        ApexPages.CurrentPage().getParameters().clear();
        StampAFRCtrlEx cx = new StampAFRCtrlEx();
        PageReference pr = cx.init();
        system.assertEquals( pr.getUrl(), '/500' );
        system.assertEquals( true, true || cx.getInDebugMode() );
        system.assertNotEquals( cx.getDebugStr(), '' );
    }
}